/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package models;

import db.Redis_Rd;
import db.Redis_W;
import java.util.Map;
import java.util.Set;
import org.json.simple.JSONValue;
import share.KeysDefinition;
import share.ShareMacros;

/**
 *
 * @author Mrkupi
 */
public class FlushData {
    
    public static void main(String[] args)
    {
        flushConfig();
        
        flushUsers();
    }
    
    /*
    Flush all data for user
    */
    public static void flushUsers()
    {
        String keyListUser      =   KeysDefinition.getKeyUserList();
        Set<String> userKeys    =   Redis_Rd.getInstance().smember( keyListUser );
        
        for( String userKey : userKeys )
        {
            flushUser( userKey );
            Redis_W.getInstance().srem(keyListUser, userKey);
        }
    }
    
    protected static void flushUser( String keyUser )
    {
        String uid  =   keyUser.replace( "U_", "" );
        
        Set<String> coteKeys    =   Redis_Rd.getInstance().smember( KeysDefinition.getKey_ListCotes(uid) );
        for( String coteKey : coteKeys )
        {
            flushCote( coteKey );
        }
        
        // Remember delete key user
        Redis_W.getInstance().del( keyUser );
    }
    
    protected static void flushCote( String coteKey )
    {
        String strCote  =   Redis_Rd.getInstance().get( coteKey );
        if( strCote != null )
        {
            Map<String, Object> mCote   =   (Map<String, Object>)JSONValue.parse(strCote);
            String keyPegs  =   String.valueOf( mCote.get( ShareMacros.PEGS ) );
            String keyEggs  =   String.valueOf( mCote.get( ShareMacros.EGGS ) );
            String keyBoxEgg=   String.valueOf( mCote.get( ShareMacros.BOXEGG ) );

            flushPegs( keyPegs );
            flushEggs( keyEggs );
            flushBoxEgg( keyBoxEgg );

            // Remember delete key cote
            Redis_W.getInstance().del( coteKey );
        }
    }
    
    protected static void flushPegs( String keyPegs )
    {
        Set<String> listPeg     =   Redis_Rd.getInstance().smember( keyPegs );
        
        for( String peg : listPeg )
        {
            Redis_W.getInstance().del( peg );
            Redis_W.getInstance().srem( keyPegs, peg );
        }
    }
    
    protected static void flushEggs( String keyEggs )
    {
        Set<String> listEggType     =   Redis_Rd.getInstance().smember( keyEggs );
        
        for( String egType : listEggType )
        {
            Set<String> listEgg     =   Redis_Rd.getInstance().smember( egType );
            for( String egg : listEgg )
            {
                Redis_W.getInstance().del( egg );
                Redis_W.getInstance().srem( egType, egg );
            }
            Redis_W.getInstance().srem( keyEggs, egType );
        }
    }
    
    protected static void flushBoxEgg( String keyBoxEgg )
    {
        String strBoxEgg    =   Redis_Rd.getInstance().get( keyBoxEgg );
        if( strBoxEgg != null )
        {
            Map<String, Object> mBoxEgg   =   (Map<String, Object>)JSONValue.parse(strBoxEgg);
            String keyEggs      =   String.valueOf( mBoxEgg.get( ShareMacros.EGGS ) );

            flushEggs(keyEggs);
        
            // Delete box egg
            Redis_W.getInstance().del( keyBoxEgg );
        }
    }
    
    protected static void flushFriend( String friendKey )
    {
    }
    
    public static void flushConfig()
    {
        String keyConfig        =   "config";
        
        Set<String> listConf    =   Redis_Rd.getInstance().smember( keyConfig );
        for( String str : listConf )
        {
            Redis_W.getInstance().del(str);
            Redis_W.getInstance().srem(keyConfig, str);
        }
    }
    
}
